	<?php
		$ordered_flags = array();
		foreach($allflags as $row):
			$ordered_flags[$row->parent][$row->id] = $row->name_en. " - " .$row->name_ar;
		endforeach;

		function check_slected_tag($selected_flags,$id)
		{
			if(isset($selected_flags) && is_array($selected_flags))
			foreach($selected_flags as $row)
				if($row->flag_id == $id)
					return TRUE;
			
			return False;
		}
		
		function build_tree($all_flags_array, $start_with=0, $selected_flags_array){
			echo "<ul class='block-list'>";
			foreach ($all_flags_array[$start_with] as $index=>$value){
				$checked = (check_slected_tag($selected_flags_array,$index) ? ' checked="checked"' : '');
				echo "<li id='li_{$index}'>";
				echo '<input id="flag_'.$index.'" onClick="check_tags('.$index.')" type="checkbox" name="checkbox[]" value="'.$index.'" '.$checked.'/>';
				echo '<label for="flag_'.$index.'"> '.$value;
				echo '</label>';
				
				echo ($checked=='' ? "<div class='d_{$index}' style='display:none'>" : "<div class='d_{$index}' style=''>");
					
					if(isset($all_flags_array[$index]) && is_array($all_flags_array[$index])){
						build_tree($all_flags_array,$index,$selected_flags_array);
					}
				echo "</div>";
				echo "</li>";
				
			}
			echo "</ul>";
		}
		
		?>

	<div id="main-content">
			<div class="container_12">
				<div class="grid_12">	

		<div class="_100">
			<div class="block-content">
				<h3>Current Selected Flags:</h3>
				<ul class="block-list">
					
				<?php if(!$selected_flags):?>
					<li><div class="alert error"><strong>Error:</strong> No Flags Selected Yet!</div></li>
				<?php else:?>
				
				<?php foreach($selected_flags as $row):?>
					<li><?php echo $row->name_en?> - <?php echo $row->name_ar?></li>
				<?php endforeach;?>
				<?php endif;?>
				</ul>
			</div>	
		</div>	
		<div class="clear"></div>
		<br>
		
				<div class="_100">
					<div class="block-content">
						<h3>Add or Edit Flags:</h3>
						<form id="form" name="form" action="<?php echo site_url($form_action)?>">
						<input type="hidden" id="id" name="id" value="<?php echo $id?>" />
						<div class="clear"></div>
						<div class="block-actions">
							<ul class="actions-left">
								<li><a class="button red" id="reset-validate-form" href="javascript:void(0);">Reset</a></li>
							</ul>
							<ul class="actions-right">
								<li><input type="submit" class="button" value="Save!"></li>
							</ul>
						</div>
						<div class="clear"></div>
						
						<div id="accordion">
							<?php build_tree($ordered_flags,0,$selected_flags);?>
						</div>
							
							<?php 
							/*<ul class="block-list">
							foreach($flags as $row):?>
								<li id="li_<?php echo $row->id?>">
									<label>
									<input onclick="check_tags(<?php echo $row->id?>)" type="checkbox" name="checkbox[]" value="<?php echo $row->id?>" />
									<?php echo $row->name_en?> - <?php echo $row->name_ar?>
									</label>
									<span class="child" id="child_<?php echo $row->id?>"></span>
								</li>
							<?php endforeach;
							</ul>*/
							?>
						</form>
					</div>
				</div>
		
		
			<div class="clear"></div>
	</div>		
	</div>		
	</div>		
			
	<style>
	.block-list .block-list li{background:#593636;color:#fff;}
	.block-list .block-list li:hover{background:#000;color:#fff;}
	.block-list .block-list .block-list li{background:#59aaa6;color:#fff;}
	.block-list .block-list .block-list li:hover{background:#590006;color:#fff;}
	</style>			
	
	<script type="text/javascript">
						
	$().ready(function() {
		
				
		/** Submit Form **/
		$("#form").validate({
				rules:{},
				messages: {},
				submitHandler: function(form) {
				   //submit
				$.post("<?php echo site_url($form_action)?>", $("#form").serialize(),
					function(data){
						if(data==1){
							
							newdata = $.parseJSON(data);
							
							if(newdata.error){
								Gtheme = 'error';
								
								$.each(newdata.msg, function(index) {
									$.jGrowl(newdata.msg[index], { theme: Gtheme,position:'center'  });
								});

							}
							else{
								Gtheme = 'saved';
								$.jGrowl('Flags Added', { theme: Gtheme,position:'center'  });
								$("#dialog").dialog('close');
							}

						}
						else{
							$.jGrowl("Error!", { theme: 'error' });
						}
					});
				}
			});
		/** End Submit Form **/
		
		
	});
	
	function check_tags(id){
		$('#li_'+id).effect("highlight", {}, 1500);
		
		x = $('#flag_'+id);
		if(x.attr('checked'))
			$('.d_'+id).fadeIn();
		else{
			$('.d_'+id).fadeOut();
			$('.d_'+id+' input').removeAttr('checked');
			$('.d_'+id+' div').fadeOut();
		}
	}
		
    </script>


		
		
		

























